package es.cea.agendatelefonica;

import java.io.IOException;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class GuardarNuevoContactoServlet extends HttpServlet {
       
    public GuardarNuevoContactoServlet() {
      
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		ContactoTel contactoNuevo = new ContactoTel();
		
		EntityManagerFactory entityManagerFactory = (EntityManagerFactory) request.getSession().getServletContext().getAttribute("emf");
		EntityManager entityManager = entityManagerFactory.createEntityManager();
		EntityTransaction transaction = entityManager.getTransaction();
		
		contactoNuevo.setNombre(request.getParameter(AgendaAtributos.nombre.toString()));
		contactoNuevo.setTelefono(request.getParameter(AgendaAtributos.telefono.toString()));
		
		transaction.begin();
		entityManager.persist(contactoNuevo);
		transaction.commit();
		
		
		request.getRequestDispatcher("nuevoContactoGuardado.jsp").forward(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
